
#include    <stdio.h>
#include    <stdarg.h>

#include    "qdxc/connection.hpp"

#include    "help/microtime.hpp"
#include    "help/logger.hpp"

using namespace    std;
using namespace    QDX::Client;
using namespace    help;

static    const char*    sg_pszServerIP = "127.0.0.1";
static    int            sg_iServerPort = 4344;
static    const char*    sg_pszMyName   = "ant";

void    do_test(Connection* pQDXC, int iTimes = 1)
{
    Logger&    logger = Logger::getInstance();

    microtime_t    timeBegin = microtime();

    for( int i=0; i<iTimes; i++ ) {    
        try {
            pQDXC->connect(sg_pszServerIP, sg_iServerPort, sg_pszMyName);
        }
        catch(exception& ex) {
            logger.error("CONNECT Exception: %s", ex.what());
            
            // skip following actions 
            continue;
        }   
    
        try {    
            pQDXC->disconnect();
        }
        catch(exception& ex) {
            logger.error("DISCONNECT Exception: %s", ex.what());
        }
        
        if ( (i>0) && (0 == (i % 1000)) ) {
            microtime_t    timeEnd = microtime();
            microtime_t    timeLast = timeEnd - timeBegin;
            double    tps = i / double(timeLast);
            logger.info("CONNECT-DISCONNECT %8d rounds last %7.3lf seconds (tps=%.2lf).", i, double(timeLast), tps);
        }
    }
    
    microtime_t    timeEnd = microtime();
    microtime_t    timeLast = timeEnd - timeBegin;
    if (iTimes > 10) {
        double    tps = iTimes / double(timeLast);
        logger.info("CONNECT-DISCONNECT %8d rounds last %7.3lf seconds (tps=%.2lf).", iTimes, double(timeLast), tps);
    }   
}

int    main( int argc, char* argv[] )
{
    Logger&    logger = Logger::getInstance();
    
    logger.info("==== test begin ====");
    
    Connection*    pQDXC = Connection::create();
    
    do_test(pQDXC);
    do_test(pQDXC, 10000);
    
    logger.info("==== test   end ====");
    
    return    0;
}


